package com.mmd.base.gobal;

import com.mmd.base.exception.BaseRuntimeException;
import com.mmd.base.exception.RpcRuntimeException;
import com.mmd.core.enums.ResultEnum;
import com.mmd.base.exception.BaseException;
import com.mmd.core.pojo.vo.BaseVO;
import com.mmd.core.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.rpc.RpcException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestControllerAdvice
public class GlobalException {
    /*Get 统一参数验证捕获*/
    @ExceptionHandler(value = BindException.class)
    public BaseVO handleExceptionsGet(Exception e, HttpServletRequest req) {
        ResultEnum resultEnum = ResultEnum.FAIL_PARAM;
        if (e instanceof BindException) {
            BindException exception = (BindException) e;
            for (ObjectError allError : exception.getAllErrors()) {
                resultEnum.setMessage(allError.getDefaultMessage());
                return Result.custom(resultEnum);
            }
            resultEnum = ResultEnum.FAIL_PARAM;
        } else {
            resultEnum.setMessage("系统级别错误::" + e.getMessage());
        }
        return Result.custom(resultEnum);
    }

    /*Post 统一参数验证捕获*/
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public BaseVO handleExceptionsPost(Exception e, HttpServletRequest req) {
        ResultEnum resultEnum = ResultEnum.FAIL_PARAM;
        if (e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException exception = (MethodArgumentNotValidException) e;
            for (ObjectError allError : exception.getBindingResult().getAllErrors()) {
                resultEnum.setMessage(allError.getDefaultMessage());
                return Result.custom(resultEnum);
            }
            resultEnum = ResultEnum.FAIL_PARAM;
        } else {
            resultEnum.setMessage("系统级别错误::" + e.getMessage());
        }
        return Result.custom(resultEnum);
    }

    @ExceptionHandler(value = BaseException.class)
    public BaseVO handleExceptionBase(BaseException e) {
        log.error("自定义异常:: "+e.getCode() + "===>" +e.getMessage());
        return Result.custom(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(value = IllegalArgumentException.class)
    public BaseVO handleExceptionRun(IllegalArgumentException e) {
        log.error("系统异常:: "+ ResultEnum.ERROR.getCode() + "===>" +e.getMessage());
        ResultEnum resultEnum = ResultEnum.messageResultEnum(e.getMessage());
        if(resultEnum.getMessage().equals(e.getMessage())){
            return Result.custom(resultEnum.getCode(),resultEnum.getMessage());
        }
        return Result.custom(resultEnum.getCode(),e.getMessage());
    }

    @ExceptionHandler(value = BaseRuntimeException.class)
    public BaseVO handleExceptionRun(BaseRuntimeException e) {
        log.error("运行时异常:: "+ ResultEnum.ERROR.getCode() + "===>" +e.getMessage());
        ResultEnum resultEnum = ResultEnum.messageResultEnum(e.getMessage());
        if(resultEnum.getMessage().equals(e.getMessage())){
            return Result.custom(resultEnum.getCode(),resultEnum.getMessage());
        }
        return Result.custom(resultEnum.getCode(),e.getMessage());
    }

    @ExceptionHandler(value = RpcException.class)
    public BaseVO handleExceptionRun(RpcException e) {
        log.error("RPC服务异常,RpcException");
        return Result.custom(ResultEnum.ERROR_NETWORK.getCode(),ResultEnum.ERROR_NETWORK.getMessage());
    }

    @ExceptionHandler(value = RpcRuntimeException.class)
    public BaseVO handleExceptionRun(RpcRuntimeException e) {
        log.error("RPC服务异常,自定义Mock实现类抛出异常RpcRuntimeException");
        return Result.custom(ResultEnum.ERROR_NETWORK.getCode(),ResultEnum.ERROR_NETWORK.getMessage());
    }
}
